package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.pojo.ShopCollect;
import com.bdqn.service.ShopCollectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收藏
 *
 * @author lilibo
 * @date 2021/10/09
 */
@Api(tags="收藏接口")
@RestController
@RequestMapping("/shopcollect")
public class ShopCollectController {

    @Autowired
    private ShopCollectService shopCollectService;

    /**
     * 新增收藏记录
     *
     * @param shopCollect
     * @return R
     */
    @ApiOperation("新增收藏记录")
    @WebLogger("新增收藏记录")
    @PostMapping("/add")
    public R save(@ApiParam("收藏对象") @RequestBody ShopCollect shopCollect) {
        return new R<>(shopCollectService.save(shopCollect));
    }

    /**
     * 修改收藏记录
     *
     * @param shopCollect
     * @return R
     */
    @ApiOperation("修改收藏记录")
    @WebLogger("修改收藏记录")
    @PostMapping("/update")
    public R update(@ApiParam("收藏对象") @RequestBody ShopCollect shopCollect) {
        return new R<>(shopCollectService.updateById(shopCollect));
    }

    /**
     * 通过id删除一条收藏记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id删除一条收藏记录")
    @WebLogger("通过id删除一条收藏记录")
    @PostMapping("/delete")
    public R removeById(@ApiParam("收藏id") Integer id) {
        return new R<>(shopCollectService.removeById(id));
    }

    /**
     * 通过id查询单条收藏记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id查询单条收藏记录")
    @WebLogger("通过id查询单条收藏记录")
    @GetMapping("/{id}")
    public R<ShopCollect> getById(@ApiParam("收藏id") @PathVariable Integer id) {
        return new R<>(shopCollectService.getById(id));
    }

    /**
     * 通过收藏信息查询收藏记录
     *
     * @param shopCollect 收藏信息对象
     * @return R
     */
    @ApiOperation("通过收藏信息查询收藏记录")
    @WebLogger("通过收藏信息查询收藏记录")
    @GetMapping("/list")
    public R<List<ShopCollect>> getList(@ApiParam("查询的收藏对象") ShopCollect shopCollect) {
        return new R<>(shopCollectService.getList(shopCollect));
    }

    /**
     * 分页查询收藏
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopCollect 收藏
     * @return
     */
    @ApiOperation("分页查询收藏")
    @WebLogger("分页查询收藏")
    @GetMapping("/page")
    public R<IPage<ShopCollect>> getPage(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的收藏对象") ShopCollect shopCollect) {
        return new R<>(shopCollectService.getPage(new Page<>(pageNo, pageSize), shopCollect));
    }

    /**
     * 分页查询收藏数据
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopCollect 收藏
     * @return 返回对象中的count为数据总量
     */
    @ApiOperation("分页查询收藏数据")
    @WebLogger("分页查询收藏数据")
    @GetMapping("/pagelist")
    public R<List<ShopCollect>> getPageList(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的收藏对象") ShopCollect shopCollect) {
        IPage<ShopCollect> page = shopCollectService.getPage(new Page<>(pageNo, pageSize), shopCollect);
        return new R<>(page.getRecords(), page.getTotal());
    }

}
